package com.huluobo.high3videoproject.mvp.login.view;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.huluobo.base.view.BaseActivity;
import com.huluobo.base.view.Event;
import com.huluobo.high3videoproject.MainActivity;
import com.huluobo.high3videoproject.R;
import com.huluobo.high3videoproject.bean.UserBeanResult;
import com.huluobo.high3videoproject.bean.UserInfo;
import com.huluobo.high3videoproject.bean.UserMoney;
import com.huluobo.high3videoproject.mvp.login.presenter.LoginContract;
import com.huluobo.high3videoproject.mvp.login.presenter.LoginPresenter;
import com.huluobo.high3videoproject.net.Constant;
import com.huluobo.high3videoproject.utils.DBUtils;
import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;

import org.greenrobot.eventbus.EventBus;

public class LoginActivity extends BaseActivity<LoginContract.View, LoginPresenter> implements LoginContract.View {

    private EditText username, password;

    @Override
    public void onLoginSuccess(UserBeanResult userBeanResult) {
        EventBus.getDefault().post(new Event("MineFragment", userBeanResult, 100));
        if (userBeanResult.getData() != null) {
            SPUtils.getInstance().put(Constant.SP_USERNAME, userBeanResult.getData().getUsername());
            SPUtils.getInstance().put(Constant.SP_USERID, userBeanResult.getData().getId());
            SPUtils.getInstance().put(Constant.SP_TOKEN, userBeanResult.getData().getToken());
            //用户登录时保存用户的钱数
            UserMoney userMoney = new UserMoney(null, userBeanResult.getData().getNickname(), "10000");
            //判断数据库中没有当前用户,则给钱
            if (DBUtils.getInstance().queryUser(userBeanResult.getData().getNickname()) == null) {
                //如果db中查询不到当前登录用户
                DBUtils.getInstance().insertUser(userMoney);
            }
            UserBeanResult.UserBean userBean = userBeanResult.getData();
            if (DBUtils.getInstance().queryUserInfo(userBean.getNickname()) == null) {
                UserInfo userInfo = new UserInfo(null,userBean.getUsername(), userBean.isAdmin(), (String) userBean.getEmail(),
                        userBean.getIcon(), userBean.getNickname(), (String) userBean.getSex(),
                        (String) userBean.getBirth(), (String) userBean.getAddress());
                DBUtils.getInstance().insertUserInfo(userInfo);
            }
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
            //登录后拿到用户,并且给MainActivity发送user
            EMClient.getInstance().login(userBeanResult.getData().getUsername(),
                    userBeanResult.getData().getPassword(), new EMCallBack() {
                        @Override
                        public void onSuccess() {
                            Log.i("TagA", "登录环信成功");
                        }

                        @Override
                        public void onError(int code, String error) {
                            Log.i("TagA", "登录环信失败:" + error);
                        }
                    });
            finish();
        }
    }

    @Override
    public void onLoginFailed(String error) {
        ToastUtils.showLong("登录失败:" + error);
    }

    @Override
    protected void initData() {
        presenter = new LoginPresenter();
        if (EMClient.getInstance().isLoggedInBefore()) {
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
        }
    }

    public void login(View view) {
        String usernameString = username.getText().toString();
        String passwordString = password.getText().toString();
        presenter.login(usernameString, passwordString);
    }

    @Override
    protected void initView() {
        username = findViewById(R.id.login_username);
        password = findViewById(R.id.login_password);
    }

    @Override
    protected int bindLayoutId() {
        return R.layout.activity_login;
    }
}